#!/usr/bin/evn python
# coding=utf-8

import hashlib
import hmac
import binascii
import base64
from typing import Union

from Crypto.Cipher import AES  # PyCrypto 库安装  安装 PyCrypto3.6问题比较多 ，直接安装pycryptodome
from binascii import b2a_hex, a2b_hex


def md5(text):
    """md5加密函数"""
    md5 = hashlib.md5()
    if not isinstance(text, bytes):
        text = str(text).encode('utf-8')
    md5.update(text)
    return md5.hexdigest()


def md5v2(content: Union[str, bytes, bytearray, memoryview]):
    """md5加密函数"""
    md5 = hashlib.md5()
    if not isinstance(content, bytes):
        content = str(content).encode('utf-8')
    md5.update(content)
    return md5.hexdigest()


def hmac_md5(content: Union[str, bytes, bytearray, memoryview], key: bytes = b'') -> str:
    """
    md5 encrypt with hmac

    :param content: words
    :param key: secret
    :return: encrypted words
    """
    if isinstance(content, str):
        content = content.encode()
    m = hmac.new(key, content, digestmod=hashlib.md5)
    return m.hexdigest()


def sha1(content: Union[str, bytes, bytearray, memoryview]) -> str:
    """
    sha1 encrypt

    :param content: words
    :return: encrypted codes
    """
    sha = hashlib.sha1()
    if isinstance(content, str):
        content = content.encode()
    sha.update(content)
    return sha.hexdigest()


def hmac_sha1_v1(key, text):
    """进行hmac加密"""
    h = hmac.new(bytes(key, encoding="utf-8"))
    h.update(bytes(text, encoding="utf-8"))
    return h.hexdigest()


def hmac_sha1(content: Union[str, bytes, bytearray, memoryview], key: bytes = b'') -> str:
    """
    sha1 encrypt with hmac

    :param content: words
    :param key: secret
    :return: encrypted words
    """
    if isinstance(content, str):
        content = content.encode()
    m = hmac.new(key, content, digestmod=hashlib.sha1)
    return m.hexdigest()


def sha256(content: Union[str, bytes, bytearray, memoryview]) -> str:
    """
    sha256 encrypt

    :param content: words
    :return: encrypted codes
    """
    sha = hashlib.sha256()
    if isinstance(content, str):
        content = content.encode()
    sha.update(content)
    return sha.hexdigest()


def hmac_sha256(content: Union[str, bytes, bytearray, memoryview], key: bytes = b'') -> str:
    """
    sha256 encrypt with hmac

    :param content: words
    :param key: secret
    :return: encrypted words
    """
    if isinstance(content, str):
        content = content.encode()
    m = hmac.new(key, content, digestmod=hashlib.sha256)
    return m.hexdigest()


def base64_encode(text):
    """进行base64编码处理"""
    return base64.b64encode(bytes(text, encoding="utf-8"))


def base64_decode(text):
    """进行base64解码处理"""
    return base64.b64decode(text)


def b64encode(content: Union[str, bytes, bytearray, memoryview]) -> bytes:
    """
    base64 encode

    :param content: words
    :return: encrypted codes
    """
    if isinstance(content, str):
        content = content.encode()
    return base64.b64encode(content)


def b64decode(content: Union[str, bytes, bytearray, memoryview]) -> bytes:
    """
    base64 decode

    :param content: codes
    :return: decrypted words
    """
    if isinstance(content, str):
        content = content.encode()
    return base64.b64decode(content)


def hmac_sha1_forpphp2(key, text):
    """进行hmac加密"""
    # h = base64.b64encode(hmac.new(bytes(key, encoding="utf-8"), msg=bytes(text, encoding="utf-8"), digestmod=hashlib.sha1).digest())
    # h = base64.b64encode(hmac.new(bytes(key, encoding="utf-8"), msg=bytes(text, encoding="utf-8"), digestmod=hashlib.sha1).digest())

    return md5(
        hmac.new(bytes(key, encoding="utf-8"), msg=bytes(text, encoding="utf-8"), digestmod=hashlib.sha1).digest())


def hmac_sha1_3(key, text):
    """进行hmac加密"""
    dk = hashlib.pbkdf2_hmac(hash_name='sha1',
                             password=bytes(key, encoding='utf-8'),
                             salt=bytes(text, encoding='utf-8'),
                             iterations=100000)
    return binascii.hexlify(dk)


def hmac_sha1_4(key, text):
    """进行hmac加密"""
    h = hmac.new(bytes(key, encoding='utf-8'), bytes(text, encoding='utf-8'), digestmod=hashlib.sha1)
    return h.hexdigest()


def hmac_sha1_5(key, text):
    return binascii.hexlify(
        hmac.new(bytes(key, encoding='utf-8'), bytes(text, encoding='utf-8'), digestmod=hashlib.sha1).digest())


def aes_encode(key, text):
    """进行AES加密"""
    pad_it = lambda s: s + (16 - len(s) % 16) * '\0'
    generator = AES.new(key, AES.MODE_CBC, b'0000000000000000')
    crypt = generator.encrypt(pad_it(text))
    cryptedStr = base64.b64encode(crypt)
    return bytes.decode(cryptedStr)


def aes_decode(key, text):
    """AES解密，解密后，去掉补足的空格用strip() 去掉"""
    generator = AES.new(key, AES.MODE_CBC, b'0000000000000000')
    cryptedStr = base64.b64decode(text)
    recovery = bytes.decode(generator.decrypt(cryptedStr))
    decryptedStr = recovery.rstrip('\0')
    return decryptedStr


if __name__ == '__main__':
    print(aes_decode('xiaozhong', aes_encode('xiaozhong', 'aaa')))
